import plugin from '../../../lib/plugins/plugin.js'
import { resolve } from 'path'
import fs from 'fs'

export class xiyangHelp extends plugin {
  constructor() {
    super({
      name: "夕阳帮助",
      dsc: "图片版功能菜单",
      event: "message",
      priority: 500,
      rule: [
        {
          reg: "^#?夕阳帮助$",
          fnc: "fixedImageHelp"
        }
      ]
    })

    // 资源路径配置
    this.resourceDir = resolve(process.cwd(), 'resources')
    this.backgroundPath = resolve(this.resourceDir, 'images/背景图.png') // 背景图必须存在
    this.outputDir = resolve(this.resourceDir, 'images/outputs')
    this.htmlTemplate = resolve(this.resourceDir, 'template/xiyang-help.html')
    
    // 初始化模板
    this.initTemplate()
  }

  /** 初始化HTML模板 */
  initTemplate() {
    if (!fs.existsSync(this.htmlTemplate)) {
      fs.mkdirSync(resolve(this.resourceDir, 'template'), { recursive: true })
      fs.writeFileSync(this.htmlTemplate, this.generateHTML(), 'utf8')
    }
  }

  /** 生成HTML内容 */
  generateHTML() {
    return `<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <style>
    body { 
      margin: 0;
      background: url('file://${this.backgroundPath}');
      background-size: cover;
      height: 100vh;
      font-family: 'Microsoft YaHei';
      padding: 50px;
      color: white;
    }
    .container {
      max-width: 1200px;
      margin: 0 auto;
    }
    .title { 
      font-size: 48px; 
      text-align: center;
      margin-bottom: 40px;
      text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
    }
    .menu-grid {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 30px;
      font-size: 32px;
    }
    .footer {
      position: absolute;
      bottom: 50px;
      width: 100%;
      text-align: center;
      font-size: 24px;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="title">≡ 夕阳娱乐功能 ≡</div>
    <div class="menu-grid">
      <div>点歌系统</div><div>视频菜单</div>
      <div>证件菜单</div><div>表白系统</div>
      <div>个性名片</div><div>查火车票</div>
      <div>查询黄金</div><div>排麦系统</div>
      <div>个人信息</div><div>图片系统</div>
      <div>绕口令</div><div>AI系统</div>
      <div>查询端口</div><div>星座信息</div>
      <div>查手机号</div><div>运行状态</div>
      <div>元梦活动</div><div>出售微信</div>
      <div>主人系统</div>
    </div>
    <div class="footer">作者：夕阳 QQ：87294576</div>
  </div>
</body>
</html>`
  }

  async fixedImageHelp() {
    try {
      // 使用云崽内置渲染引擎
      const outputPath = await this.renderWithYunzai()
      await this.reply([segment.image(`file://${outputPath}`)])
      return true
    } catch (err) {
      console.error('[夕阳帮助] 生成失败:', err)
      await this.reply('功能菜单生成失败，请联系管理员')
      return false
    }
  }

  /** 调用云崽内置截图 */
  async renderWithYunzai() {
    const outputPath = resolve(this.outputDir, `help-${Date.now()}.png`)
    
    // 使用云崽的 render 方法
    await this.e.render({
      tplFile: this.htmlTemplate,
      saveId: 'xiyang-help',
      savePath: outputPath,
      width: 1200,
      height: 1800,
      setViewport: {
        deviceScaleFactor: 2,
        isMobile: false
      }
    })
    
    return outputPath
  }
}
